package com.example.zk2cr;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ListView lv;
    List<Music> list = new ArrayList<>();
    MusicAdapter musicAdapter;
    ContentResolver cr;
    Uri uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        musicAdapter = new MusicAdapter(this,list);
        lv.setAdapter(musicAdapter);
        cr = getContentResolver();
        uri = Uri.parse("content://com.bw.music");

        //点击弹出修改框
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_diy, null);
                EditText tvId = inflate.findViewById(R.id.et_id);
                EditText tvName = inflate.findViewById(R.id.et_name);
                EditText tvDuration = inflate.findViewById(R.id.et_duration);
                dialog = new AlertDialog.Builder(MainActivity.this)
                        .setView(inflate)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //点击确定，进行添加
                                ContentValues values = new ContentValues();

                                values.put("name",tvName.getText().toString());
                                values.put("duration",tvDuration.getText().toString());
                               cr.update(uri,values,"id=?",new String[]{tvId.getText().toString()});
                            }
                        })
                        .create();
                dialog.show();
            }
        });
        //长按删除
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                cr.delete(uri,"id=?",new String[]{list.get(position).getId()+""});
                list.remove(position);
                musicAdapter.notifyDataSetChanged();
                return true;
            }
        });

    }
    AlertDialog dialog;
    public void insert(View view) {
        View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_diy, null);
        TextView tvId = inflate.findViewById(R.id.et_id);
        TextView tvName = inflate.findViewById(R.id.et_name);
        TextView tvDuration = inflate.findViewById(R.id.et_duration);
        dialog = new AlertDialog.Builder(this)
                .setView(inflate)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //点击确定，进行添加
                        ContentValues values = new ContentValues();
                        values.put("id",tvId.getText().toString());
                        values.put("name",tvName.getText().toString());
                        values.put("duration",tvDuration.getText().toString());
                        list.add(new Music(Integer.parseInt(tvId.getText().toString()),tvName.getText().toString(),Integer.parseInt(tvDuration.getText().toString())));
                        musicAdapter.notifyDataSetChanged();
                        cr.insert(uri,values);
                    }
                })
                .create();
        dialog.show();
    }

    public void query(View view) {
        list.clear();
        Cursor cursor = cr.query(uri, null, null, null, null);
        if(cursor!=null){
            while (cursor.moveToNext()){
                @SuppressLint("Range") int id = cursor.getInt(cursor.getColumnIndex("id"));
                @SuppressLint("Range") int duration = cursor.getInt(cursor.getColumnIndex("duration"));
                @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("name"));
                list.add(new Music(id,name,duration));
            }
            musicAdapter.notifyDataSetChanged();
        }
    }

    private void initView() {
        lv = (ListView) findViewById(R.id.lv);
    }
}